// import { getCommentDetail } from '../../../../services/good/comments/fetchCommentDetail';
import Toast from 'tdesign-miniprogram/toast/index';
import { api } from '../../../../config/api';
import { request } from '../../../../utils/request';
Page({
	data: {
		orderId: '',
		serviceRateValue: 5,
		goodRateValue: 5,
		conveyRateValue: 5,
		isAnonymous: false,
		//本地文件集合
		uploadFiles: [],
		//已上传文件集合
		uploadedFiles: [],
		gridConfig: {
			width: 218,
			height: 218,
			column: 3,
		},
		isAllowedSubmit: false,
		imgUrl: '',
		title: '',
		goodsDetail: '',
		imageProps: {
			mode: 'aspectFit',
		},
	},

	onLoad(options) {
		this.setData({
			imgUrl: options.imgUrl,
			title: options.title,
			goodsDetail: options.specs,
			orderId: options.orderId
		});
	},

	onRateChange(e) {
		const { value } = e?.detail;
		const item = e?.currentTarget?.dataset?.item;
		this.setData({ [item]: value }, () => {
			this.updateButtonStatus();
		});
	},

	onAnonymousChange(e) {
		const status = !!e?.detail?.checked;
		this.setData({ isAnonymous: status });
	},

	handleSuccess(e) {
		const { files } = e.detail;
		const me = this;
		const hasFileSizeLimited = files.some(x => {
			//10M限制
			return x.size > 10485760;
		});
		if (hasFileSizeLimited) {
			me.showTips('附件大小不能超过10M');
			return;
		}
		let uploadFiles = [];
		wx.showLoading({
			title: '上传中',
		});
		files.forEach(fileItem => {
			const token = wx.getStorageSync("token");
			wx.uploadFile({
				url: api.user.uploadHeader,
				filePath: fileItem.url,
				name: 'file',
				header: {
					"Authorization": `Bearer ${token}`,
					"Content-Type": "application/json"
				},
				success: function (res) {
					let data = JSON.parse(res.data);
					uploadFiles.push({ src: data.data.src, type: fileItem.type });
					wx.hideLoading();
				},
				fail: function (error) {
					console.log('上传失败：', error);
					wx.hideLoading();
					me.showTips('上传失败');
				}
			});
		});
		this.setData({
			uploadFiles: files,
			uploadedFiles: uploadFiles
		});
	},

	handleRemove(e) {
		const { index } = e.detail;
		const { uploadFiles } = this.data;
		uploadFiles.splice(index, 1);
		this.setData({
			uploadFiles
		});
	},

	onTextAreaChange(e) {
		const value = e?.detail?.value;
		this.textAreaValue = value;
		this.updateButtonStatus();
	},

	updateButtonStatus() {
		const { serviceRateValue, goodRateValue, conveyRateValue, isAllowedSubmit } = this.data;
		const { textAreaValue } = this;
		const temp = serviceRateValue && goodRateValue && conveyRateValue && textAreaValue;
		if (temp !== isAllowedSubmit) this.setData({ isAllowedSubmit: temp });
	},

	onSubmitBtnClick() {
		const { isAllowedSubmit } = this.data;
		if (!isAllowedSubmit) return;
		const me = this;
		request.post(api.order.comment, this.data)
			.then(res => {
				me.showTips('评价提交成功');
				wx.navigateBack();
			});
	},
	showTips(msg) {
		Toast({
			context: this,
			selector: '#t-toast',
			message: msg,
			theme: 'success',
		});
	}
});
